package com.onpositive.datamap.core.impl;

import java.util.Date;

import com.onpositive.data.IPropertyConstants;
import com.onpositive.data.IPropertyMetadata;
import com.onpositive.data.diskstorage.AbstractValueAccessor;
import com.onpositive.data.units.DateWithAccuracy;
import com.onpositive.datamap.IDataPropertyValue;
import com.onpositive.datamap.core.IDataProperty;
import com.onpositive.datamap.core.IDateProperty;
import com.onpositive.datamap.values.impl.DateRange;

public class DateProperty extends DataProperty implements IDataProperty,IDateProperty{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public DateProperty(IPropertyMetadata meta,
			AbstractValueAccessor collection) {
		super(meta, collection);
	}

	
	@Override
	public IDataPropertyValue convert(Object value) {
		if (value!=null){
			return new DateRange(this, null, (DateWithAccuracy) value);
		}
		return null;
	}


	@Override
	public String simpleType() {
		return IPropertyConstants.PROP_KIND_DATE;
	}


	@Override
	public Object adaptFilterValue(String c) {
		if (c.equals("-")){
			return new DateWithAccuracy(0);
		}
		int indexOf = c.indexOf('-');
		if (indexOf!=-1){
			String substring = c.substring(0,indexOf);
			String substring1 = c.substring(indexOf+1);
			DateWithAccuracy parse = DateWithAccuracy.parseSimpleDate(substring);
			DateWithAccuracy parse1 = DateWithAccuracy.parseSimpleDate(substring1);
			return new DateRange(this, null, parse,parse1);
		}
		if (c.charAt(0)=='!'){
			return new Date(Long.parseLong(c.substring(1)));
		}		
		return DateWithAccuracy.parseSimpleDate(c);
	}


	@Override
	public IDataProperty clone(AbstractValueAccessor va) {
		return new DateProperty(meta, va);
	}

	
	
}
